<?php
/*************
#系统用户管理
#lsj 2010-11-2
************/

class sysuserCtrl extends lib_ctrl
{
	public $Cname = '系统用户管理';
	protected $user = null;

	/*
	#初始化
	*/
	function _init()
	{
            parent::_init();
            $this->user = $this->loadModel('user');
			
	}

	/*
	#用户列表
	*/
	function indexAct()
	{
		$userName = strip_tags(trim($_GET['userName']));
		$userEmail = strip_tags(trim($_GET['userEmail']));
		$page = abs(intval($_GET['page']));#页码数
        $perNum = 15;#每页显示条数

		if($userName != ''){
			$this->view->assign('userName', $userName);
			$userName = $this->user->escapeLike($userName);
			$this->user->setName($userName);
		}
		if($userEmail != ''){
			$this->view->assign('userEmail', $userEmail);
			$userEmail = $this->user->escapeLike($userEmail);
			$this->user->setEmail($userEmail);
		}

		$result = $this->user->getUser($perNum);#获取列表信息
		$pager = $this->user->pager($page);#生成页码栏

		$this->view->assign('result', $result);
		$this->view->assign('pager', $pager);
		$this->view->display();
	}


	/*
	#登陆
	*/
	function loginAct()
	{
		#已登陆，跳到首页
		if($this->user->isLogin())
		{
			if($this->isAjax()){
                exit('100');
			}else {
				$this->redirect(DOMAIN_DOOR);
				exit;
			}
		}

		#AJAX登录
        if(!empty($_POST)){
            session_start();
            $userName = strip_tags(trim($_POST['login_name']));
            $userPaswd = trim($_POST['login_passwd']);
            $userCaptcha = strtolower(strip_tags(trim($_POST['login_captcha'])));

            if($userCaptcha == '') exit('-10');
            if($userName == '') exit('-11');
            if($userPaswd == '') exit('-12');
            if(strtolower($_SESSION['login']) != $userCaptcha) exit('-13');

            $result = $this->user->login($userName, $userPaswd);

            if(true === $result){
                unset($_SESSION['login']);
                exit('1');
            }
            echo $result;
            exit;
        }

        $backurl = strip_tags(trim($this->getParam('backurl')));
        if(!$backurl) $backurl = DOMAIN_DOOR;
		
        $this->view->assign('TITLE', '请登录系统');
        $this->view->assign('backurl', $backurl);
        $this->view->display();
	}

	/*
	#退出
	*/
	function logoutAct()
	{
		$this->user->logout();
        $this->redirect(DOMAIN_DOOR);
	}

	/*
    #添加用户
    */
    function addAct()
    {
        #AJAX提交注册
        if(!empty($_POST)){
			$userConfig = $this->loadConfig('user');#加载与用户相关的配置文件
			$item = $this->user->filterItem($_POST['item']);
			
            if('' == $item['name']) exit('-10');#
            if(!preg_match($userConfig['name_pat'], $item['name'])) exit('-11');#用户名格式不正确

            if('' == $item['email']) exit('-12');#
            if(!preg_match($userConfig['email_pat'], $item['email'])) exit('-13');#邮箱格式不正确

            if('' == $item['truename']) exit('-14');#
            if('' == $item['passwd']) exit('-15');#
            
            $plen = strlen($item['passwd']);
			if($plen < 6 || $plen > 20) exit('-16');#用户密码长度不对

            $item['createtime'] = $_SERVER['REQUEST_TIME'];
            $item['addip'] = getIp();
            $password = $item['passwd'];
            $item['passwd'] = $this->user->createPasswd($item['passwd']);

            //$this->user->setDebug();
            $res = $this->user->addUser($item);

            #发送通知邮件
            if($res){
                $item['passwd'] = $password;
                $item['door'] = getUrl('user', 'login');
            	$this->view->assign('info', $item);
            	$text = $this->view->fetch('sysuser', 'mail_add');#邮件内容
            	$text = explode("\n", $text, 2);#区分出邮件标题与内容
            	$subject = trim($text[0]);#第一行为邮件标题
            	$content = $text[1];#邮件内容
            	if(!sendMail($subject, $content, $info['email'])) exit('-18');
            	exit('1');
			}
            exit($res);
        }

        $group = $this->loadModel('group');
        $res = $group->getInfo();

        $this->view->assign('group', $res);
        $this->view->display();
    }


    /*
    #修改用户信息
 	#一般是由管理进行操作
    */
    function editInfoAct()
    {
        #AJAX提交注册
        if(!empty($_POST)){
            $userConfig = $this->loadConfig('user');#加载与用户相关的配置文件
			$item = $this->user->filterItem($_POST['item']);
			
            if('' == $item['id']) exit('-10');#
			if('' == $item['name']) exit('-11');#
            if(!preg_match($userConfig['name_pat'], $item['name'])) exit('-12');#用户名格式不正确

            if('' == $item['email']) exit('-13');#
            if(!preg_match($userConfig['email_pat'], $item['email'])) exit('-14');#邮箱格式不正确

            if('' == $item['truename']) exit('-15');#

			if('' == $item['passwd']) unset($item['passwd']);
			else{
				$plen = strlen($item['passwd']);
				if($plen < 6 || $plen > 20) exit('-16');#用户密码长度不对
                $password = $item['passwd'];
                $item['passwd'] = $this->user->createPasswd($item['passwd']);
			}

            $item['edittime'] = $_SERVER['REQUEST_TIME'];
            
            //$this->user->setDebug();
            $res = $this->user->editUser($item);

            #发送通知邮件
            if($res){
                if('' != $item['passwd']) $item['passwd'] = $password;
            	$this->view->assign('info', $item);
            	$text = $this->view->fetch('sysuser', 'mail_eidt');#邮件内容
            	$text = explode("\n", $text, 2);#区分出邮件标题与内容
            	$subject = trim($text[0]);#第一行为邮件标题
            	$content = $text[1];#邮件内容
            	if(!sendMail($subject, $content, $info['email'])) exit('-14');
            	exit('1');
			}
            exit($res);
        }
        
        $id = abs(intval($_GET['id']));
        if(!$id) $this->forward('sysindex', 'error', array('msg'=>'缺少用户ID','link'=>1));
        //$this->user->setDebug();
		$res = $this->user->setId($id)->getUser(1);
		$gp = $this->loadModel('group');
        $group = $gp->getInfo();

        $this->view->assign('group', $group);
		
		$this->view->assign('res', $res);
        $this->view->display();
    }

    /*
    #本人修改密码
	#本人只能修改密码，不能修改信息
    */
    function editPaswdAct()
    {
        if(!empty($_POST)){
        	$uid = abs(intval($_POST['uid']));
            $oldPaswd = trim($_POST['oldPaswd']);
            $newPaswd1 = trim($_POST['newPaswd1']);
            $newPaswd2 = trim($_POST['newPaswd2']);

            if(!$oldPaswd) exit('-10');#
            if(strlen($oldPaswd) > 20 || strlen($oldPaswd) < 6) exit('-11');
            if(!$newPaswd1) exit('-12');
            if(strlen($newPaswd1) > 20 || strlen($newPaswd1) < 6) exit('-13');
            if(!$newPaswd2) exit('-14');
            if(strlen($newPaswd2) > 20 || strlen($newPaswd2) < 6) exit('-15');
            if($newPaswd1 != $newPaswd2) exit('-16');

            $res = $this->user->editPasswd($oldPaswd, $newPaswd1, $uid);
            if($res) exit('1');
            exit($res);
        }

        $this->view->display();
    }

    /*
    #删除用户
    */
    function delAct()
    {
        if(!empty($_POST)){
	        $uid = abs(intval($_POST['id']));
	        if(!$uid)exit('-11');

            $res = $this->user->delUser($uid);
            if($res) exit('1');
            exit($res);
        }
        exit('-10');
    }
    
    /**
     * 生成随机的初始密码
     */
    function getWordAct()
    {
    	$num = strtolower(getRandNum(6, 'all'));
    	exit($num);
    }


}